-- 创建主框架
local frame = CreateFrame("Frame", "MultiShotIndicator", UIParent)
frame:SetSize(40, 40)
frame:SetPoint("CENTER", 0, 0)

-- 创建图标纹理
local icon = frame:CreateTexture(nil, "OVERLAY")
icon:SetAllPoints(true)
icon:SetTexture("Interface\\Icons\\Ability_UpgradeMoonGlaive") -- 多重射击图标
icon:Hide()

-- 设置更新频率（秒）
local updateInterval = 0.2
local elapsed = 0

-- 主要更新函数
frame:SetScript("OnUpdate", function(self, delta)
    elapsed = elapsed + delta
    if elapsed < updateInterval then return end
    elapsed = 0

    local targetExists = UnitExists("target")
    local spellName = "多重射击"

    -- 检查是否在战斗中且目标是敌对
    if targetExists and UnitCanAttack("player", "target") then
        local count = 1  -- 总是计算当前目标
        local maxCount = 3

        -- 检查名称板可见的敌人
        for _, plate in pairs(C_NamePlate.GetNamePlates()) do
            local unit = plate.namePlateUnitToken
            if unit and UnitCanAttack("player", unit) and not UnitIsUnit(unit, "target") then
                local distanceSq = UnitDistanceSquared("target", unit)
                if distanceSq and distanceSq <= 64 then  -- 8码距离平方（8^2=64）
                    count = count + 1
                    if count >= maxCount then
                        icon:Show()
                        return
                    end
                end
            end
        end
    end

    icon:Hide()
end)

-- 添加拖动支持（可选）
frame:SetMovable(true)
frame:EnableMouse(true)
frame:RegisterForDrag("LeftButton")
frame:SetScript("OnDragStart", frame.StartMoving)
frame:SetScript("OnDragStop", frame.StopMovingOrSizing)

-- 打印加载信息
print("多重射击检测插件已加载！满足条件时将显示图标。")